/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
// stop abstract class name check
package com.genia.toolbox.basics.exception;

import java.util.Arrays;

import com.genia.toolbox.basics.i18n.I18nMessage;
import com.genia.toolbox.basics.i18n.I18nMessageFactory;

/**
 * this class represents the ancestor of all exception classes on toolbox the
 * goal is to make a bundled exception message.
 */
@SuppressWarnings("serial")
public abstract class BundledException
    // NOPMD
    extends Exception
{

  /**
   * default key used in the internationalization properties file.
   */
  public static final String DEFAULT_KEY = "com.genia.toolbox.basics.exception.BundledException.NoMessage";

  /**
   * default exception message.
   */
  public static final String DEFAULT_MESSAGE = "exception";

  /**
   * stores the message on the exception object.
   */
  private transient I18nMessage i18nMessage;



  /**
   * default constructor.
   */
  public BundledException()
  {
    this(DEFAULT_MESSAGE, DEFAULT_KEY);
  }



  /**
   * constructor with the key of the message.
   * 
   * @param key
   *          the key used in the internationalization properties file
   */
  public BundledException(final String key)
  {
    this(DEFAULT_MESSAGE, key);
  }



  /**
   * Constructor from message, the key of the message and the arguments to
   * constructs the message.
   * 
   * @param message
   *          exception message
   * @param key
   *          the key used in the internationalization properties file
   * @param arguments
   *          exception arguments to send
   */
  public BundledException(final String message, final String key, final Object... arguments)
  {
    super(message);
    this.i18nMessage = I18nMessageFactory.getIl8nMessage(key);
    initArguments(arguments);
  }



  /**
   * constructor from an existing <code>Throwable</code>.
   * 
   * @param cause
   *          the underlying throwable cause
   */
  public BundledException(final Throwable cause)
  {
    this(cause, DEFAULT_KEY);
  }



  /**
   * Constructor from an existing <code>Throwable</code>, the key of the
   * message and the arguments to constructs the message.
   * 
   * @param cause
   *          the underlying throwable cause
   * @param key
   *          the key used in the internationalization properties file
   * @param arguments
   *          exception arguments to send with the exception object
   */
  public BundledException(final Throwable cause, final String key, final Object... arguments)
  {
    super(cause);
    this.i18nMessage = I18nMessageFactory.getIl8nMessage(key);
    initArguments(arguments);
  }



  /**
   * this method initializes the arguments of the exception.
   * 
   * @param arguments
   *          exception arguments to send
   */
  private void initArguments(final Object... arguments)
  {
    i18nMessage.getParameters().add(getMessage());
    i18nMessage.getParameters().addAll(Arrays.asList(arguments));
  }



  /**
   * getter for the i18nMessage property. This method return the informations
   * needed to generate a localized error message.
   * 
   * @return the i18nMessage
   */
  public I18nMessage getI18nMessage()
  {
    return i18nMessage;
  }

}
// resume abstract class name check
